package es.udc.emarketapp.http.controller.actions.clients;

import es.udc.emarketapp.emarket.model.client.vo.ClientVO;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import es.udc.emarketapp.emarket.model.usersFacade.UsersFacade;
import es.udc.emarketapp.emarket.model.usersFacade.util.PasswordEncrypter;
import es.udc.emarketapp.emarket.util.exceptions.model.NifCifAlreadyExistsException;
import es.udc.emarketapp.emarket.util.exceptions.model.UserNameAlreadyExistsException;
import es.udc.emarketapp.http.view.actionForms.ClientForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
import java.util.Calendar;

public class RegisterClientAction extends DefaultAction {

    final static String CLIENT = "CLIENT";

    public ActionForward doExecute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        /* Get data. */

        ClientForm regsterClientForm = (ClientForm) form;
        String idClient = regsterClientForm.getIdClient();
        String pass = regsterClientForm.getPass();
        String name = regsterClientForm.getName();
        String surname1 = regsterClientForm.getSurname1();
        String surname2 = regsterClientForm.getSurname2();
        String nifCif = regsterClientForm.getNifCif();
        String email = regsterClientForm.getEmail();
        String tlf1 = regsterClientForm.getTlf1();
        String tlf2 = regsterClientForm.getTlf2();
        Calendar birthDate = regsterClientForm.getBirthDate();
        String address = regsterClientForm.getAddress();
        String city = regsterClientForm.getCity();
        String cp = regsterClientForm.getCp();
        String province = regsterClientForm.getProvince();
        String country = regsterClientForm.getCountry();

        //boolean sameAddress = regsterClientForm.getSameAddress();
        String addressSend = regsterClientForm.getAddressSend();
        String citySend =  regsterClientForm.getCpSend();
        String cpSend = regsterClientForm.getCpSend();
        String provinceSend = regsterClientForm.getProvinceSend();
        String countrySend  = regsterClientForm.getCountrySend();
        
       

        /*
        if (!(sameAddress)) {
            addressSend = regsterClientForm.getAddressSend();
            citySend = regsterClientForm.getCitySend();
            cpSend = regsterClientForm.getCpSend();
            provinceSend = regsterClientForm.getProvinceSend();
            countrySend = regsterClientForm.getCountrySend();
        } else {
            addressSend = address;
            citySend = city;
            cpSend = cp;
            provinceSend = province;
            countrySend = country;
        }
                 
            addressSend = regsterClientForm.getAddressSend();
            citySend = regsterClientForm.getCitySend();
            cpSend = regsterClientForm.getCpSend();
            provinceSend = regsterClientForm.getProvinceSend();
            countrySend = regsterClientForm.getCountrySend();

         * 
         */

        /* Create client*/
        ActionMessages errors = new ActionMessages();
        UsersFacade usersFacade = new UsersFacade();

        /* Crypt pass */

        String encryptedPass = PasswordEncrypter.crypt(pass);



        ClientVO clientVO = new ClientVO(idClient, encryptedPass, name, surname1,
                surname2, nifCif, address, city, cp, province, country, email,
                tlf1, tlf2, CLIENT, true, true, birthDate, addressSend,
                citySend, cpSend, provinceSend, countrySend);


        /*
         * Insert the ClientVO in the database, and the returned ClientVO
         * (which contains the client identifier) in the request.
         */

        try {

            ClientVO insertedClientVO = usersFacade.createClient(clientVO);

            request.setAttribute("client", insertedClientVO);

        } catch (InternalErrorException e) {
            System.out.println("Error al registrar cliente " + e);
        } catch (UserNameAlreadyExistsException e) {
            errors.add("idClient",
                    new ActionMessage("ErrorMessages.idClient.alreadyExists"));
        } catch (NifCifAlreadyExistsException e) {
            errors.add("nifCif",
                    new ActionMessage("ErrorMessages.nifCif.alreadyExists"));
        }
        /* Return ActionForward. */
        if (errors.isEmpty()) {
            //return mapping.findForward("Success");
            String type = "CLIENT_ID";
            return new ActionForward(mapping.findForward("clientDetails").getPath()+"?identifierType="+type+"&identifier="+idClient, false);

        } else {
            saveErrors(request, errors);
            return new ActionForward(mapping.getInput());
        }
    }
}
